{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c451036d-055d-4e23-b375-c471bddb27ae",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-01T03:15:30.001902Z",
     "iopub.status.busy": "2022-04-01T03:15:30.001533Z",
     "iopub.status.idle": "2022-04-01T03:15:30.111309Z",
     "shell.execute_reply": "2022-04-01T03:15:30.110588Z",
     "shell.execute_reply.started": "2022-04-01T03:15:30.001812Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3f2ce6d-192d-4a9c-af73-0b41fdeb030f",
   "metadata": {},
   "source": [
    "# 小结\n",
    "1. random.seed：设定计算伪随机数的值\n",
    "2. random.rand：生成随机浮点数/数组\n",
    "3. random.randn：生成标准正态分布随机数/数组\n",
    "4. random.randint：生成随机整数/数组\n",
    "5. random.choice：随机从序列中选取元素\n",
    "6. random.shuffle：随机打断数组中元素的顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d0e42146-0360-456a-8ded-c3188939efeb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-01T03:22:24.162165Z",
     "iopub.status.busy": "2022-04-01T03:22:24.161910Z",
     "iopub.status.idle": "2022-04-01T03:22:24.168591Z",
     "shell.execute_reply": "2022-04-01T03:22:24.167304Z",
     "shell.execute_reply.started": "2022-04-01T03:22:24.162139Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.47179023979832724"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不设置seed，直接生成随机数\n",
    "np.random.rand()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c69052da-6787-45cd-ac3d-6ef5db394c35",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-01T03:26:18.606489Z",
     "iopub.status.busy": "2022-04-01T03:26:18.606234Z",
     "iopub.status.idle": "2022-04-01T03:26:18.611255Z",
     "shell.execute_reply": "2022-04-01T03:26:18.610578Z",
     "shell.execute_reply.started": "2022-04-01T03:26:18.606462Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.417022004702574"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 设置seed，再生成随机数\n",
    "np.random.seed(1)\n",
    "np.random.rand()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e76ffca6-c890-4bb9-bfec-989374d3e53d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-01T03:28:38.345163Z",
     "iopub.status.busy": "2022-04-01T03:28:38.344915Z",
     "iopub.status.idle": "2022-04-01T03:28:38.349490Z",
     "shell.execute_reply": "2022-04-01T03:28:38.348838Z",
     "shell.execute_reply.started": "2022-04-01T03:28:38.345138Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4191945144032948"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成0-1的随机小数\n",
    "np.random.rand()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ebe2e215-1e36-46a7-a141-3f566676aa47",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-01T03:50:37.873092Z",
     "iopub.status.busy": "2022-04-01T03:50:37.872830Z",
     "iopub.status.idle": "2022-04-01T03:50:37.878730Z",
     "shell.execute_reply": "2022-04-01T03:50:37.877993Z",
     "shell.execute_reply.started": "2022-04-01T03:50:37.873066Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.62169572, 0.11474597, 0.94948926, 0.44991213, 0.57838961],\n",
       "       [0.4081368 , 0.23702698, 0.90337952, 0.57367949, 0.00287033],\n",
       "       [0.61714491, 0.3266449 , 0.5270581 , 0.8859421 , 0.35726976],\n",
       "       [0.90853515, 0.62336012, 0.01582124, 0.92943723, 0.69089692]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成随机数组\n",
    "np.random.rand(4, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "0d45a85f-0bb6-4fa9-a392-5dcf6790d6b5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-01T07:16:24.270686Z",
     "iopub.status.busy": "2022-04-01T07:16:24.270435Z",
     "iopub.status.idle": "2022-04-01T07:16:24.276565Z",
     "shell.execute_reply": "2022-04-01T07:16:24.275932Z",
     "shell.execute_reply.started": "2022-04-01T07:16:24.270660Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.3992681719195915"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成标准正态分布的随机数\n",
    "np.random.randn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "bc698efd-9d85-4b67-9a5a-8df25bc36138",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-01T07:33:23.069607Z",
     "iopub.status.busy": "2022-04-01T07:33:23.069354Z",
     "iopub.status.idle": "2022-04-01T07:33:23.075966Z",
     "shell.execute_reply": "2022-04-01T07:33:23.074836Z",
     "shell.execute_reply.started": "2022-04-01T07:33:23.069581Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成一维数组\n",
    "data = np.arange(12)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "6e80b46b-0a50-4296-ade2-7540461761f8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-01T07:35:41.104334Z",
     "iopub.status.busy": "2022-04-01T07:35:41.104065Z",
     "iopub.status.idle": "2022-04-01T07:35:41.110843Z",
     "shell.execute_reply": "2022-04-01T07:35:41.110080Z",
     "shell.execute_reply.started": "2022-04-01T07:35:41.104297Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11,  1,  1])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从data中随机取3个元素\n",
    "np.random.choice(data, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "e5d91082-e794-4ad5-8c3e-64c25ec1370f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-01T07:41:48.491420Z",
     "iopub.status.busy": "2022-04-01T07:41:48.491168Z",
     "iopub.status.idle": "2022-04-01T07:41:48.497658Z",
     "shell.execute_reply": "2022-04-01T07:41:48.496817Z",
     "shell.execute_reply.started": "2022-04-01T07:41:48.491395Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 8,  7,  0, 10],\n",
       "       [ 3,  4,  2,  0],\n",
       "       [11,  3,  5,  1]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从data中随机取元素，组成一个3行4列的数组\n",
    "np.random.choice(data, size=(3, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "729f38ed-557a-4e6a-beb2-65d310b66994",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-01T07:47:55.084122Z",
     "iopub.status.busy": "2022-04-01T07:47:55.083873Z",
     "iopub.status.idle": "2022-04-01T07:47:55.089606Z",
     "shell.execute_reply": "2022-04-01T07:47:55.088572Z",
     "shell.execute_reply.started": "2022-04-01T07:47:55.084095Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 0, 1])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从0-4中随机取3个元素\n",
    "np.random.choice(5, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "9dfc7375-22d7-490c-a60c-2f1ff9ed9a4f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-01T07:52:19.413337Z",
     "iopub.status.busy": "2022-04-01T07:52:19.413031Z",
     "iopub.status.idle": "2022-04-01T07:52:19.418424Z",
     "shell.execute_reply": "2022-04-01T07:52:19.417774Z",
     "shell.execute_reply.started": "2022-04-01T07:52:19.413310Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 4, 4, 3],\n",
       "       [3, 3, 3, 2],\n",
       "       [3, 3, 4, 3]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从0-4中随机取元素，组成一个3行4列的数组\n",
    "np.random.choice(5, size=(3, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "4968db5c-31d0-4431-bfdb-84083d943bc9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-01T08:05:20.587245Z",
     "iopub.status.busy": "2022-04-01T08:05:20.586994Z",
     "iopub.status.idle": "2022-04-01T08:05:20.592639Z",
     "shell.execute_reply": "2022-04-01T08:05:20.591696Z",
     "shell.execute_reply.started": "2022-04-01T08:05:20.587219Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成一维数组\n",
    "data1 = np.arange(10)\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "6001224d-7f4d-471f-8f5d-d626d505e27f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-01T08:05:23.876565Z",
     "iopub.status.busy": "2022-04-01T08:05:23.876276Z",
     "iopub.status.idle": "2022-04-01T08:05:23.882704Z",
     "shell.execute_reply": "2022-04-01T08:05:23.881722Z",
     "shell.execute_reply.started": "2022-04-01T08:05:23.876539Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 7, 3, 0, 4, 1, 6, 9, 8, 2])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 打乱data1中元素顺序\n",
    "np.random.shuffle(data1)\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "c971dc97-4441-49b6-ac13-fa7c5be5c447",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-01T08:14:20.172007Z",
     "iopub.status.busy": "2022-04-01T08:14:20.171756Z",
     "iopub.status.idle": "2022-04-01T08:14:20.178003Z",
     "shell.execute_reply": "2022-04-01T08:14:20.177045Z",
     "shell.execute_reply.started": "2022-04-01T08:14:20.171980Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 6, 7, 0, 3, 9],\n",
       "       [0, 3, 4, 7, 5, 3],\n",
       "       [8, 8, 0, 6, 7, 9],\n",
       "       [5, 4, 9, 5, 2, 5],\n",
       "       [6, 6, 8, 7, 7, 7]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成随机整型数组\n",
    "data2 = np.random.randint(0, 10, size=(5, 6))\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "b9b1aa66-8402-4fa3-8cbf-e1ca27513c57",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-01T08:15:13.991533Z",
     "iopub.status.busy": "2022-04-01T08:15:13.991307Z",
     "iopub.status.idle": "2022-04-01T08:15:13.997903Z",
     "shell.execute_reply": "2022-04-01T08:15:13.996797Z",
     "shell.execute_reply.started": "2022-04-01T08:15:13.991509Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 4, 9, 5, 2, 5],\n",
       "       [8, 8, 0, 6, 7, 9],\n",
       "       [6, 6, 8, 7, 7, 7],\n",
       "       [0, 3, 4, 7, 5, 3],\n",
       "       [0, 6, 7, 0, 3, 9]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 打乱data2中的行顺序\n",
    "np.random.shuffle(data2)\n",
    "data2"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
